// pages/userinfo.js
const baseUrl = require('../../config').baseUrl
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isBackstage: false, // 是否后台
        array: [],
        userInfo: '',
        companyList: [],
        username: '', // 用户完善的用户名
        deptname: '', // 用户完善的单位名称
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            userInfo: wx.getStorageSync('userInfo'),
            username: app.globalData.username,
            deptname: app.globalData.deptname,
        })
        this.getCompany()
    },
    onHide: function () {
        this.setData({
            isBackstage: true
        })
    },
    onShow () {
        if (wx.getStorageSync('isReopen')) {
            if (wx.getStorageSync('modifiUserInfo')) {
                this.setData({
                    username: app.globalData.username,
                    deptname: app.globalData.deptname
                })
                wx.setStorageSync('modifiUserInfo', '')
            }
            wx.setStorageSync('isReopen', false)
        }
        if (this.data.isBackstage) {
            this.setData({
                companyList: [],
                isBackstage: false
            })
            this.getCompany()
        }
    },
    userinfoDetail: function () {
        wx.navigateTo({
            url: '../userinfoDetail/userinfoDetail'
        })
    },
    //获取input的值
    getInputVal: function (e) {
        debugger;
        var nowIdx = e.currentTarget.dataset.idx;//获取当前索引
        var val = e.detail.value;//获取输入的值
        var oldVal = this.data.inputVal;
        oldVal[nowIdx] = val;//修改对应索引值的内容
        this.setData({
            inputVal: oldVal
        })
    },
    gotAddCompany: function () {
        wx.navigateTo({
            url: '/pages/modifierCompany/index'
        })
    },
    /**
     *  获取单位列表
     */
    getCompany () {
        let This = this
        wx.request({
            url: baseUrl,
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            data: {
                data: JSON.stringify({
                    business: "GetDeptInfoList",
                    data: {
                        account: app.globalData.account
                    }
                })
            },
            method: 'POST',
            success (res) {
                if (res.data.code === 0) {
                    This.setData({
                        companyList: This.data.companyList.concat(res.data.data)
                    })
                } else {
                    wx.showToast({
                        title: res.data.info,
                        icon: 'none',
                        duration: 1000
                    })
                }
            },
            fail: (error) => {
                wx.showToast({
                    title: error.data.info,
                    icon: 'none',
                    duration: 1000
                })
            }
        })
    },
})